// pages/helpEach/helpEach.js
Page({

      /**
       * 页面的初始数据
       */
      data: {
            helpList:[],
            housingInfo:{
                  id:'',
                  name:''
            },
            host:getApp().globalData.host,
            bgImg:''
      },
      publish(){
            wx.navigateTo({
                  url: "/pages/publishHelp/publishHelp",
            })
      },

      /**
       * 生命周期函数--监听页面加载
       */
      onLoad: function (options) {
            let usingHistory=wx.getStorageSync('usingHistory')||{};
            usingHistory.helpEach=new Date().getTime();
            wx.setStorageSync('usingHistory', usingHistory);
      },

      /**
       * 生命周期函数--监听页面初次渲染完成
       */
      onReady: function () {

      },

      /**
       * 生命周期函数--监听页面显示
       */
      onShow: function () {
            let userInfo=wx.getStorageSync("userInfo");
            
            if(userInfo.nickName==null || userInfo.nickName==""){
                  wx.showModal({
                        title: '未登录',
                        content: '是否进行登录？',
                        success: (res)=> {
                              if (res.confirm) {
                                    wx.navigateTo({
                                          url:'/pages/login/login'
                                    })
                              } else if (res.cancel) {
                                    // 点击取消后返回主页面
                                    wx.switchTab({
                                          url: '/pages/index/index'
                                    })
                                    wx.showToast({
                                          title: '需要登录',
                                          icon: 'error'
                                    })
                              }
                        }
                  })
            }else if(userInfo.addr == undefined || userInfo.addr == "null" || userInfo.addr == "" || userInfo.addr == null){
                  wx.showModal({
                        title: '未完善信息',
                        content: '需要补全房屋信息',
                        success: (res)=> {
                              if (res.confirm) {
                                    wx.navigateTo({
                                          url:'/pages/userInfo/userInfo'
                                    })
                              } else if (res.cancel) {
                                    // 点击取消后返回主页面
                                    wx.switchTab({
                                          url: '/pages/index/index'
                                    })
                                    wx.showToast({
                                          title: '需要完善信息',
                                          icon: 'error'
                                    })
                              }
                        }
                  })
            }else{
                  this.init()
            }
      },
      // 初始化当前页面数据
      init(){
            wx.showLoading({
              title: '加载数据'
            })
            let userInfo=wx.getStorageSync('userInfo');
            wx.request({
                  url: getApp().globalData.host+'client/user/helpList?housingId='+userInfo.housingId,
                  success:res=>{
                        let helpList=res.data.data.helpList;
                        helpList.forEach(item=>{
                              item.imgArr=JSON.parse(item.imgArr)
                              item.replyTime=this.formatTime(item.replyTime)
                        })
                        this.setData({
                              helpList:helpList
                        })
                        let housingInfo={
                              name:userInfo.housingName,
                              id:userInfo.housingId
                        }
                        this.setData({
                              housingInfo:housingInfo
                        })
                        wx.hideLoading();
                  }
            })
            this.initBg();
      },
      initBg(){
            wx.request({
                  url:'https://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=zh-CN',
                  success: res=>{
                      this.setData({
                              bgImg:'https://cn.bing.com'+res.data.images[0].url
                      })
                  },
                  fail: res=> {
                      console.log(res);
                  }
            })
      },
      formatTime(e){
            var time =  new Date(parseInt(e));
            var m = time.getMonth()+1;
            var d = time.getDate();
            let t= m+'月'+d+'日'+' '+time.getHours()+'时';
            return t;
      },
})